import axios from 'axios'

// 创建 axios 实例
const apiClient = axios.create({
//   baseURL: 'http://localhost:8080' ,
  baseURL: 'http://112.124.28.46:8080' ,
  headers: {
    'Content-Type': 'application/json'
  },
  timeout: 360000
})

// 请求拦截器（可选，用于统一处理 token 等）
apiClient.interceptors.request.use(
  config => {
    // 可以在这里添加 token
    // const token = localStorage.getItem('token')
    // if (token) config.headers.Authorization = `Bearer ${token}`
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器（可选，用于统一处理错误）
apiClient.interceptors.response.use(
  response => {
    // 如果后端统一返回格式为 { code, data, message }，可以在这里处理
    return response.data
  },
  error => {
    console.error('请求错误:', error)
    return Promise.reject(error)
  }
)

// API 接口封装
export default {
  // === 邀请函生成 ===
  async generateInvitation(input) {
    try {
      const response = await apiClient.post('/invitation/generate', { user_input: input })
      return response
    } catch (error) {
      console.error('生成邀请函失败:', error)
      throw error
    }
  },

  // === 游戏库管理 ===
  async getGamesList() {
    try {
      const response = await apiClient.get('/admin/game/list')
      return response
    } catch (error) {
      console.error('获取游戏列表失败:', error)
      throw error
    }
  },

  async createGame(gameData) {
    try {
      const response = await apiClient.post('/admin/game/create', gameData)
      return response
    } catch (error) {
      console.error('添加游戏失败:', error)
      throw error
    }
  },

  async updateGame(gameData) {
    try {
      const response = await apiClient.post('/admin/game/update', gameData)
      return response
    } catch (error) {
      console.error('更新游戏失败:', error)
      throw error
    }
  },

  async deleteGame(gameData) {
    try {
      const response = await apiClient.post('/admin/game/delete', gameData)
      return response
    } catch (error) {
      console.error('删除游戏失败:', error)
      throw error
    }
  },

  // === 导师库管理 ===
  async getTeachersList() {
    try {
      const response = await apiClient.get('/admin/teacher/list')
      return response
    } catch (error) {
      console.error('获取导师列表失败:', error)
      throw error
    }
  },

  async createTeacher(teacherData) {
    try {
      const response = await apiClient.post('/admin/teacher/create', teacherData)
      return response
    } catch (error) {
      console.error('添加导师失败:', error)
      throw error
    }
  },

  async updateTeacher(teacherData) {
    try {
      const response = await apiClient.post('/admin/teacher/update', teacherData)
      return response
    } catch (error) {
      console.error('更新导师失败:', error)
      throw error
    }
  },

  async deleteTeacher(teacherData) {
    try {
      const response = await apiClient.post('/teacher/delete', teacherData)
      return response
    } catch (error) {
      console.error('删除导师失败:', error)
      throw error
    }
  }
}
